Imports System.Xml.XPath
Imports System.Web

Public Class XmlHandler

    'Const QuestionAnswerFile As String = "~/App_Data/Qas.xml"
    Dim questionAnswer As QuizEntities.Question
    Dim xmldoc As XPathDocument '= New XPathDocument(QuestionAnswerFile)
    Dim navigate As XPathNavigator '= xmldoc.CreateNavigator()


    Public Sub New(ByVal quizFileName As String)
        xmldoc = New XPathDocument(quizFileName)
        navigate = xmldoc.CreateNavigator()
    End Sub

    Function ReadAllQuestions(ByVal fileName As String) As List(Of String)
        Dim strList As New List(Of String)
        Dim xmldoc As XPathDocument = New XPathDocument(fileName)
        Dim navi As XPathNavigator = xmldoc.CreateNavigator()
        For Each question As XPathNavigator In navi.Select("Quiz/QA")
            strList.Add(question.SelectSingleNode("Ques").Value)
        Next
        Return strList
    End Function


    Enum QAType
        Ques
        OptionOne
        OptionTwo
        OptionThree
        OptionFour
        OptionFive
        OptionSix
        CorrectAnswer
        QuesCategory
        TopicCategory
        QuizCategory
        QuesHint
        AdditionalInfo
        ExtraInfo
    End Enum


    Function MakeXPath(ByVal id As String, ByVal type As QAType) As String
        Dim QueryPath As String = String.Empty
        Select Case type
            Case QAType.Ques
                QueryPath = "Quiz/QA[QuesId=" & id & "]/Ques"
            Case QAType.OptionOne
                QueryPath = "Quiz/QA[QuesId=" & id & "]/OptionOne"
            Case QAType.OptionTwo
                QueryPath = "Quiz/QA[QuesId=" & id & "]/OptionTwo"
            Case QAType.OptionThree
                QueryPath = "Quiz/QA[QuesId=" & id & "]/OptionThree"
            Case QAType.OptionFour
                QueryPath = "Quiz/QA[QuesId=" & id & "]/OptionFour"
            Case QAType.OptionFive
                QueryPath = "Quiz/QA[QuesId=" & id & "]/OptionFive"
            Case QAType.OptionSix
                QueryPath = "Quiz/QA[QuesId=" & id & "]/OptionSix"
            Case QAType.CorrectAnswer
                QueryPath = "Quiz/QA[QuesId=" & id & "]/CorrectAnswer"
            Case QAType.QuesCategory
                QueryPath = "Quiz/QA[QuesId=" & id & "]/QuesCategory"
            Case QAType.TopicCategory
                QueryPath = "Quiz/QA[QuesId=" & id & "]/TopicCategory"
            Case QAType.QuizCategory
                QueryPath = "Quiz/QA[QuesId=" & id & "]/QuizCategory"
            Case QAType.QuesHint
                QueryPath = "Quiz/QA[QuesId=" & id & "]/QuesHint"
            Case QAType.AdditionalInfo
                QueryPath = "Quiz/QA[QuesId=" & id & "]/AdditionalInfo"
            Case QAType.ExtraInfo
                QueryPath = "Quiz/QA[QuesId=" & id & "]/ExtraInfo"
        End Select
        Return QueryPath
    End Function

    Function GetQuestionById(ByVal id As String) As QuizEntities.Question
        Try
            questionAnswer = New QuizEntities.Question()
            questionAnswer.Ques = (navigate.SelectSingleNode(MakeXPath(id, QAType.Ques)).Value)
            questionAnswer.OptionOne = (navigate.SelectSingleNode(MakeXPath(id, QAType.OptionOne)).Value)
            questionAnswer.OptionTwo = (navigate.SelectSingleNode(MakeXPath(id, QAType.OptionTwo)).Value)
            questionAnswer.OptionThree = (navigate.SelectSingleNode(MakeXPath(id, QAType.OptionThree)).Value)
            questionAnswer.OptionFour = (navigate.SelectSingleNode(MakeXPath(id, QAType.OptionFour)).Value)
            questionAnswer.OptionFive = (navigate.SelectSingleNode(MakeXPath(id, QAType.OptionFive)).Value)
            questionAnswer.OptionSix = (navigate.SelectSingleNode(MakeXPath(id, QAType.OptionSix)).Value)
            questionAnswer.CorrectAnswer = (navigate.SelectSingleNode(MakeXPath(id, QAType.CorrectAnswer)).Value)
            questionAnswer.QuesCategory = (navigate.SelectSingleNode(MakeXPath(id, QAType.QuesCategory)).Value)
            questionAnswer.TopicCategory = (navigate.SelectSingleNode(MakeXPath(id, QAType.TopicCategory)).Value)
            questionAnswer.QuizCategory = (navigate.SelectSingleNode(MakeXPath(id, QAType.QuizCategory)).Value)
            questionAnswer.QuesHint = (navigate.SelectSingleNode(MakeXPath(id, QAType.QuesHint)).Value)
            questionAnswer.AdditionalInfo = (navigate.SelectSingleNode(MakeXPath(id, QAType.AdditionalInfo)).Value)
            questionAnswer.ExtraInfo = (navigate.SelectSingleNode(MakeXPath(id, QAType.ExtraInfo)).Value)
        Catch e As Exception
            questionAnswer = Nothing
        End Try
        Return questionAnswer
    End Function

End Class
